package com.mini.repository;

import com.mini.model.BusinessRequirementPlanFormBean;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 项目名称：traditional-business
 * 类 名 称：BusinessRequirementPlanFormRepository
 * 类 描 述：TODO
 * 创建时间：2020/12/16 下午2:33
 * 创 建 人：zhanghuiying
 */
@Repository
public interface BusinessRequirementPlanFormRepository extends JpaRepository<BusinessRequirementPlanFormBean,Long>, JpaSpecificationExecutor<BusinessRequirementPlanFormBean> {
    List<BusinessRequirementPlanFormBean> findByTableIdAndFormDelete(Long tableId,Integer delete);
    @Modifying
    @Query(value = "UPDATE business_requirement_plan_form SET form_delete=1\n" +
            "WHERE table_id in (?1) AND form_delete=0 ",nativeQuery = true)
    void updateByTableIds(List<Long> tableId);

    @Query(value = "SELECT f.* FROM business_requirement_plan_base b \n" +
            "LEFT JOIN business_requirement_plan_table t ON b.id=t.base_id \n" +
            "LEFT JOIN business_requirement_plan_form f ON t.id=f.table_id \n" +
            "WHERE b.quantum_army_group_id=?1 AND b.plan_date=?2 \n" +
            "AND t.project_id=?3 AND t.table_delete=0 " +
            "AND f.channel_code=?4 AND f.form_delete=0 " +
            "AND f.province=?5 ",nativeQuery = true)
    BusinessRequirementPlanFormBean findByquantumArmyGroupIdAndPlanDateAndProjectIdAndProvince(Integer quantumArmyGroupId, Date planDate, Integer projectId,String channelCode,String province);

    @Modifying
    @Transactional
    @Query(value = "UPDATE business_requirement_plan_form \n" +
            "SET allot_count=?2 \n" +
            "WHERE id=?1 ",nativeQuery = true)
    void updateAllotCount(Long key, Integer newAllotCount);

    BusinessRequirementPlanFormBean findByTableIdAndChannelCodeAndProvince(Long id, String channelCode, String province);
}
